package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    UserService userService;

    @RequestMapping("getUserName")
    public Result getUserName(Authentication authentication){
        try {
            String username = authentication.getName();
            return new Result(true,null,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户名失败");
        }
    }

    @RequestMapping("changePassword")
    public Result changePassword(@RequestParam("username") String username,@RequestParam("password") String password){
        try {
            userService.changePassword(username,password);
            return new Result(true,"修改密码成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改密码失败");
        }
    }

    // 此方法用作模板引擎登录
    @RequestMapping("login")
    public ModelAndView login(String error){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("---login---");
        modelAndView.addObject("error",error);
        modelAndView.setViewName("mylogin");
        return  modelAndView;
    }
}
